package com.swk.loadResources;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类加载器加载资源文件
 */
@WebServlet("/LoadByCL")
public class LoadByCL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoadByCL() {
        super();
    }

	/**
	 * 此时/表示:/WEB-INF/classes/目录
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		//文件在IDE工具的src目录下
//		InputStream is = cl.getResourceAsStream("/config1.properties");
		InputStream is = cl.getResourceAsStream("/com/swk/config/config2.properties");
		Properties prop = new Properties();
		prop.load(is);
		System.out.println(prop.getProperty("name"));
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

}
